(function ($) {

    $.fn.extend({
        "citylist": function (params) {
            params = $.extend({
                id: 'id',                   //数据的ID名称
                name: 'name',                //数据的VALUE名称
                children: [],        //数据子元素的KEY的名称
                metaTag: 'name',       //将数据元素的ID存放到HTML元素的标签中
                idVal: false,               //使用ID做OPTION的VALUE
                data: false,                 //默认数据
                selected: false              //选中的元素eg: 14 / [14:1401]
            }, params);

            var target = $(this);
            var hasSelected = ' selected="selected"';
            var html = [];

            if (!target.length) {
                return this;
            } else {
                var province = target.eq(0);
                var city = target.eq(1);
				var area = target.eq(2);
				//省
                var html = [], oItem;
                html.push('<option name="0" value="">请选择</option>');
                for (var item in params.data) {
                    oItem = params.data[item];
                    if (params.idVal) {

                        html.push('<option ' + params.metaTag + '="' + oItem[params.id] + '" value="' + oItem[params.name] + '"' + ((params.selected && ($.trim(params.selected[0]) == $.trim(oItem[params.name]))) ? hasSelected : '') + '>' + oItem[params.name] + '</option>');

                    } else {
                        html.push('<option ' + params.metaTag + '="' + oItem[params.id] + '" value="' + oItem[params.name] + '"' + ((params.selected && ($.trim(params.selected[0]) == $.trim(oItem[params.name]))) ? hasSelected : '') + '>' + oItem[params.name] + '</option>');
                    }
                }
                html = html.join('');
                province.find('option').remove();
                province.append(html);

				//市
                var provinces = province.find('option');
                province.on('change',function () {
                    var curSelect = $(this).val();
                    provinces.each(function (k, v) {
                        if ($(v).val() == curSelect) {
                            return (function (v) {
                                var extra = $(v).attr(params.metaTag);
                                var html = [], oItem;
                                html.push('<option name="0" value="">请选择</option>');
                                for (var item in params.data) {
                                    oItem = params.data[item];
                                    if (oItem[params.id] == extra && oItem[params.children[0]]) {
                                        oItem = oItem[params.children[0]];
                                        for (var sItem in oItem) {
                                            if (params.idVal) {
                                                html.push('<option ' + params.metaTag + '="' + oItem[sItem][params.id] + '" value="' + oItem[sItem][params.name] + '"' + ((params.selected && ($.trim(params.selected[1]) == $.trim(oItem[sItem][params.name]))) ? hasSelected : '') + '>' + oItem[sItem][params.name] + '</option>');

                                            } else {
                                                html.push('<option ' + params.metaTag + '="' + oItem[sItem][params.id] + '" value="' + oItem[sItem][params.name] + '"' + ((params.selected && ($.trim(params.selected[1]) == $.trim(oItem[sItem][params.name]))) ? hasSelected : '') + '>' + oItem[sItem][params.name] + '</option>');

                                            }
                                        }
                                        break;
                                    }
                                }
                                html = html.join('');
                                city.find('option').remove();
                                city.append(html);

								//区、县
								var citys = city.find('option');
								city.on('change',function () {
									var curSelect = $(this).val();
									citys.each(function (k, v) {
										if ($(v).val() == curSelect) {
											return (function (v) {
												var extra = $(v).attr(params.metaTag);
												var html = [], oItem;
                                                html.push('<option name="0" value="">请选择</option>');
												for (var item in params.data) {
													for (var c_item in params.data[item][params.children[0]]) {
														oItem = params.data[item][params.children[0]];
														if (oItem[c_item][params.id] == extra && oItem[c_item][params.children[1]]) {
															oItem = oItem[c_item][params.children[1]];
															for (var sItem in oItem) {
																if (params.idVal) {
																	html.push('<option ' + params.metaTag + '="' + oItem[sItem][params.id] + '" value="' + oItem[sItem][params.name] + '"' + ((params.selected && ($.trim(params.selected[2]) == $.trim(oItem[sItem][params.name]))) ? hasSelected : '') + '>' + oItem[sItem][params.name] + '</option>');

																} else {
																	html.push('<option ' + params.metaTag + '="' + oItem[sItem][params.id] + '" value="' + oItem[sItem][params.name] + '"' + ((params.selected && ($.trim(params.selected[2]) == $.trim(oItem[sItem][params.name]))) ? hasSelected : '') + '>' + oItem[sItem][params.name] + '</option>');

																}
															}
															break;
														}
													}
												}
												html = html.join('');
												area.find('option').remove();
												area.append(html);
											}(v));
										}
									})

								}).trigger('change');

                            }(v));
                        }
                    })

                }).trigger('change');

            }
            return this;
        }
    });
})(jQuery);